package com.rice.commonsdk.template;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 封装验证工具类
 * @author 米饭好好吃
 */
public class ValidateTemplate {

    /**
     * 判断字符串中是否包含特殊字符
     * @param str 输入字符串
     * @return true: 存在 false: 不存在
     */
    public boolean hasSpecialCharacters(String str) {
        String regex = "^[a-zA-Z0-9]+$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        return !matcher.matches();
    }

    /**
     * 校验邮箱格式是否正确
     * @param email 输入邮箱字符串
     * @return true: 正确 false: 错误
     */
    public boolean isEmailValid(String email) {
        String emailRegex = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";
        Pattern pattern = Pattern.compile(emailRegex);
        Matcher matcher = pattern.matcher(email);
        return matcher.matches();
    }

    /**
     * 校验电话号码格式是否正确
     * @param phone 输入电话号码字符串
     * @return true: 正确 false: 错误
     */
    public boolean isPhoneValid(String phone) {
        String phoneRegex = "^1[3-9]\\d{9}$";
        Pattern pattern = Pattern.compile(phoneRegex);
        Matcher matcher = pattern.matcher(phone);
        return matcher.matches();
    }
}
